package org.sunshine.dye.service.info.impl;

import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.sunshine.dye.dao.info.CustomerDao;
import org.sunshine.dye.dto.CustomerDto;
import org.sunshine.dye.exception.BusinessException;
import org.sunshine.dye.service.info.CustomerService;
import org.sunshine.dye.util.Utils;

@Service
@Transactional //声明本业务类的所有方法都是事务性的
public class CustomerServiceImpl implements CustomerService {
	
	
	private int currId;
	
	
	
	@Autowired
	private CustomerDao customerDao;

	
	@PostConstruct//定义初始化方法
	public void init() {
		currId = customerDao.findMaxId();
	}
	
	private synchronized Integer newId() {
		return ++currId;
	}
	
	/*
	 {
	 	list:[{},{},...],             rows
	 	pageSize:10,//每页记录数     
	 	size:10,//当前页实际记录数   currSize
	 	pageNum:3,
	 	prePage:2, pre
	 	nextPage:4,next
	 	firstPage:1,first
	 	pages:10, pgTotal
	 	total:109,
	 
	 }
	 */	
	@Override
	public Map<String,Object> getCustomerList(CustomerDto dto) {
		
		//创建了一个【Object的无名子类】对象
		//Object obj = new Object() {};
		
		//创建了一个【接口的无名实现类】对象
//		QueryAction<Customer> action = new QueryAction<Customer>() {
//
//			@Override
//			public List<Customer> query() {
//				// TODO Auto-generated method stub
//				return testDao.findCustomerList(dto);
//			}
//			
//		};
//		
//		Utils.getPage(dto,action);
		
		
		
//		Utils.getPage(dto,new QueryAction<Customer>() {
//			@Override
//			public List<Customer> query() {
//				return testDao.findCustomerList(dto);
//			}			
//		});
		
		return Utils.getPage(dto, ()->{return customerDao.findCustomerList(dto);});
		
		
//		
//		PageHelper.startPage(dto);
//		List<Customer> list = testDao.findCustomerList(dto);
//		PageInfo<Customer> pageInfo = new PageInfo<Customer>(list);
//		
//		Map<String,Object> map = new HashMap<String,Object>();
//		map.put("rows", pageInfo.getList());
//		map.put("pageSize", pageInfo.getPageSize());
//		map.put("currSize", pageInfo.getSize());
//		map.put("pageNum", pageInfo.getPageNum());
//		map.put("pre", pageInfo.getPrePage());
//		map.put("next", pageInfo.getNextPage());
//		map.put("first", 1);
//		map.put("last", pageInfo.getPages());
//		map.put("pgTotal", pageInfo.getPages());
//		map.put("total", pageInfo.getTotal());
//		
//		return map;
//		
	}

	@Override
	public void addCustomer(CustomerDto dto) {
		dto.setCus_id(newId());
		customerDao.insertOneCustomer(dto);
		
	}

	@Override
	public void updateCustomer(CustomerDto dto) {
		customerDao.updateOneCustomer(dto);
		
	}

	@Override
	public void deleteCustomer(Integer... id) {
		if(id==null || id.length==0) {
			throw new BusinessException("用户没有选择删除的数据！");
		}
		customerDao.deleteCustomers(id);
		
	}
	
	

}
